<template>
  <div>
    <a-modal :visible="show" title="个人中心" :width="600" okText="提交" @ok="submit" :confirm-loading="loading" @cancel="close">
      <a-form-model ref="form" :model="form" :rules="fuzhiRules" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-form-model-item label="账号" >
          <a-input v-model="username" disabled/>
        </a-form-model-item>
        <a-form-model-item label="原密码" >
          <a-input-password v-model="form.password" placeholder="请输入原密码" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="新密码" prop="repassword">
          <a-input-password v-model="form.repassword" placeholder="请输入新密码，包含大小写字符、数字及特殊符号，且不小于8位" autocomplete="off"/>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
import {updatePass} from '@/api/modules/permission'

let checkPassword = (rule, value, callback) => {
  //密码判断
  if (value != '') {
    if (value.trim().length < 8) {  
      callback(new Error('密码长度不符合标准'));
    }else if (!/^\S+/.test(value) || !/\S+$/.test(value)){
      callback(new Error('密码不符合规范'));
    }else{
      let level = 0
      if(/\d+/.test(value)){level++}
      if(/[a-z]+/.test(value)){level++}
      if(/[A-Z]+/.test(value)){level++}
      if (!/^[\u4e00-\u9fa5_a-zA-Z0-9]+$/.test(value)){level++}
      if(level < 4){
        callback(new Error('密码强度弱'));
      }else{
        callback()
      }
    }
  }else{
    callback(new Error('密码不符合规范'));
  }
};

export default {
  props:{
    show: Boolean,
    username: String,
  },
  data(){
    return{
      loading:false,
      form:{
        password:'',
        repassword:'',
      },
      labelCol: { span: 3 },
      wrapperCol: { span: 21 },
      fuzhiRules:{
        repassword:[
          { validator: checkPassword, trigger: 'change',require:true },
        ],
      },
    }
  },
  methods:{
    submit(){
      this.$refs.form.validate((valid)=>{
        if(valid){
          this.loading = true
          let form = JSON.parse(JSON.stringify(this.form))
          form.password = form.password.trim()
          form.repassword = form.repassword.trim()
          // console.log(form)
          updatePass(form).then((res) => {
            this.loading = false
            if(res.errcode == 0){
              this.$message.success('修改成功')
              this.close()
            }
          }).catch(() => {
            this.loading = false
          });
        }
      })
    },
    close(){
      this.$emit('update:show',false)
    }
  },
  watch:{
    
  }
}
</script>